#include "EventPoller.h"
#include <SDL.h>
#include "AntTweakBar.h"
#include <assert.h>

void EventPoller::Update()
{
	SDL_Event event;
	Handlers handlers = m_handlers;
	int handled = 0;

	while (SDL_PollEvent(&event))
	{
		if (event.type == SDL_QUIT)
		{
			exit(0);
		}

		handled = TwEventSDL(&event, SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
		if(!handled)  // if event has not been handled by AntTweakBar, process it
		{
			for (Handlers::iterator it = handlers.begin(); it != handlers.end(); it++)
			{
				(*it)->HandleEvent(event);
			}
		}


	}
}

void EventPoller::AddHandler(EventHandler* eh)
{
	m_handlers.insert(eh);
}

void EventPoller::RemoveHandler(EventHandler* eh)
{
	assert(m_handlers.find(eh) != m_handlers.end());

	m_handlers.erase(eh);
}